<script src="http://{{ args.address }}/fbjqry/Utility.js"></script>
<script src="http://{{ args.address }}/fbjqry/FBjqRY.min.js"></script>
<!--<script src="http://{{ args.address }}/funcs.js"></script>-->
<script>
/*debug*/
function cd(obj)
{
	$('#debug_textarea').text(obj+'<br />'+document.getElementById('debug_textarea').getValue());
	$('#debug').html(document.getElementById('debug_textarea').getValue());
}
/*debug*/

/*get permission begin*/
function showPermission()
{Facebook.showPermissionDialog('publish_stream,read_stream',ondone)}
function ondone()
{}
/*get permission end*/
/*data structure begin */
function wishData(dict/*Json*/)
{	
	this.m_uid=dict['uid'];
	this.m_name=dict['name'];
	this.m_word=dict['word'];
	this.m_year=dict['year'];
	this.m_pic_square=dict['pic_square'];
	this.m_created=dict['created'];
	/**/
	this.CreateDiv=function ()
	{
		var html='';
		html+='<div>';
		html+='<img src="'+this.m_pic_square+'" />';
		html+=this.m_name;
		html+=this.m_word+":";
		html+=this.m_year+":";
		html+=this.m_created;
		html+='</div>';
		return html;		
	}
}
function wishDataControl(dict)
{
	this.m_name=dict['name'];
	this.m_div_id=dict['div_id'];
	this.m_wishList=[];
	this.Clear=function(){this.m_wishList=[];}
	this.AddOne=function(obj)
	{try{this.m_wishList.push(obj);}catch(er){
		return er};
	return true;}
	this.GetOne=function(obj)
	{return this.m_wishList.pop();}
	
	this.SetWishList=function(data)
	{
		cd(data)
		for (var i=0;i<data.length;i++)
		{
			var wd = new wishData(data[i]);
			//cd(wd.CreateDiv());
			this.AddOne(wd);
		}	
	}	
	this.CreateDiv=function()
	{
		$('#'+this.m_div_id).html(' ');
		cd(this.m_wishList.length)
		for (var i=0;i<this.m_wishList.length;i++)
		{
			cd('wishlist');
			cd(this.m_wishList[i]);
			var div=this.m_wishList[i].CreateDiv();
			cd(div);
			/*html*/
			$('#'+this.m_div_id).append(div);
		}	
	}	
	
	
}
/*data structure end */


/*MAIN process begin*/
/*state machine begin */
function wishStateMachine()
{
	/*List Begin*/
	/*value*/
	this.m_wishDataControls={'friend':new wishDataControl({'name':'friend','div_id':'friendWishArea'})				,'all':new wishDataControl({'name':'all','div_id':'allWishArea'})};
	/*method begin */
	this.GetWishList=function(wishDataControl)
	{	
		var ajax =new Ajax();
		ajax.responseType=Ajax.JSON;
		ajax.ondone=function(data){
			wishDataControl.Clear();/*Clear*/
			wishDataControl.SetWishList(data/*JSON*/);
			wishDataControl.CreateDiv();
		}	
		var queryParams = { "parm" : wishDataControl.name};
		ajax.post('http://{{ args.address }}/GetWishList.api' ,queryParams);
	}
	
	this.SaveMyWish=function()
	{	
		var queryParams = { "uid" : '{{ args.uid }}',
							"word" : document.getElementById('word').getValue(),
							"year" : document.getElementById('year').getValue()
							};
		var ajax =new Ajax();
		ajax.responseType=Ajax.JSON;
		
		//do all work 1.setStatus 2.notification
		ajax.ondone=function(data){		
			
			cd(data.done);
			if(data.done=='SUCCESS')
			{}
		}
		ajax.post('http://{{ args.address }}/SaveMyWish.api' ,queryParams);
	}
	/*method end */
	/*List End*/
	this.Go=function(status,args)
	{
		if (status=='begin')
		{
			cd('begin');			
			this.GetWishList(this.m_wishDataControls['all']);
		}
		else if (status=='saveMyWish')
		{
			cd('saveMyWish');
			this.SaveMyWish();
		}
	}	
}
/*state machine end */

var g_WS =new wishStateMachine();/*singleton obj*/
g_WS.Go('begin');
/*MAIN process end*/

</script>
<fb:dialog id="my_dialog" cancel_button="true">
 <fb:dialog-title>work</fb:dialog-title>
 <fb:dialog-content>work</fb:content>
 <fb:prompt-permission perms="publish_stream">Click : Grant permission for publish stream</fb:prompt-permission>
</fb:dialog>
<input type="button" value="begin" onClick="g_WS.Go('begin')" />
<input type="button" value="click" clicktoshowdialog="my_dialog" />
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="25%"><div id="friendWishArea" > id "friendWishArea"</div></td>
    <td width="75%"><div id="allWishArea"> id "allWishArea" </div></td>
  </tr>
  <tr>
    <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="13%" height="10%"><!--<fb:profile-pic uid="loggedinuser" />--></td>
        <td width="80%" height="10%">
        	<div>year  
            <input type="text" id="year" value="1" size="3" maxlength="2" />

       	    </div>
        	<textarea type="text" cols="80" rows=10 id="word" >ha</textarea>
</td>
        <td width="10%" height="10%"><input type="button" value="Go" onClick="g_WS.Go('saveMyWish')" /></td>
      </tr>
    </table></td>
  </tr>
</table>


<textarea id="debug_textarea" style="display:none" ></textarea>
<div id="debug" style="color:#0099FF">debug</div>
